exports.getCaptcha = function(req, res) {

    req.session.captcha = parseInt(Math.random() * 9000 + 1000);

    var captchapng = require('captchapng');
    var p = new captchapng(70, 40, req.session.captcha); // width,height,numeric captcha

    p.color(0, 0, 0, 0); // First color: background (red, green, blue, alpha)
    p.color(80, 80, 80, 255); // Second color: paint (red, green, blue, alpha)

    var img = p.getBase64();
    var imgbase64 = new Buffer(img, 'base64');

    res.type('png');
    res.send(imgbase64);
    res.end();

}

exports.checkCaptcha = function(req, res, next) {
    if (req.body.code != req.session.captcha) {
        req.session.captcha = null;
        req.session.msg = {
            type: 'error',
            msg: '验证码不正确'
        }

        res.redirect('back');
    } else {
        next();

    }

}
